package product;

import java.awt.EventQueue;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;

import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JButton;

import product.ProductData;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MemberMonth extends JInternalFrame {
	HashMap<String, TempData> chul;
	HashMap<String, MemberData> mem;
	DefaultTableModel model;
	TableColumn column = null;
	int Rowday = 0;
	int rday = 0;
	String[][] memchul;
	String[] r;
	String[] s;
	int i=0;

	private JScrollPane scrollPane;
	private JTable table;
	private JComboBox comboBox;
	private JComboBox comboBox_1;
	private JTextField textField;
	private JButton btnNewButton;
	private JButton button;
	private JButton button_1;

	/**
	 * Launch the application.
	 */

	// public static void main(String[] args) {
	// EventQueue.invokeLater(new Runnable() {
	// public void run() {
	// try {
	// MemberMonth frame = new MemberMonth();
	// frame.setVisible(true);
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }
	// });
	// }

	/**
	 * Create the frame.
	 */
	public MemberMonth(HashMap<String, TempData> chul, 
								HashMap<String, MemberData> mem) {
		super("월별현황", true, true, true, false);
		this.chul = chul;
		this.mem = mem;

		Vector<String> colNames = new Vector<String>(); // 벡터에 테이블항목을 저장하기위해서
														// colNames 생성.
		colNames.add("이름");
		for (int i = 1; i <= 31; i++) {
			colNames.add(i + "일");
		}

		// colNames.add("사번");
		// colNames.add("출근");
		// colNames.add("결근");
		// colNames.add("월차");
		// colNames.add("연차");
		// colNames.add("생리");
		// colNames.add("기타");

		model = new DefaultTableModel(colNames, 0); // (콜네임, 행의갯수).

		setBounds(100, 100, 1050, 517);
		getContentPane().setLayout(null);
		getContentPane().add(getScrollPane());
		getContentPane().add(getComboBox());
		getContentPane().add(getComboBox_1_1());
		getContentPane().add(getTextField());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getButton());
		getContentPane().add(getButton_1());

		for (int j = 0; j < 32; j++) {
			column = table.getColumnModel().getColumn(j);
			if (j == 0) {
				column.setPreferredWidth(77); // sport column is bigger
			} else {
				column.setPreferredWidth(30);
			}
		}
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 37, 1010, 366);
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}

	private JTable getTable() {
		if (table == null) {
			table = new JTable(model);
			table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		}
		return table;
	}

	private JComboBox getComboBox() {
		if (comboBox == null) {
			comboBox = new JComboBox();
			comboBox.setBounds(12, 10, 71, 21);

			for (int i = 1; i < 2; i++) {
				for (int j = 0; j < 5; j++) {
					comboBox.addItem("20" + i + j);
				}
			}
		}
		return comboBox;
	}

	private JComboBox getComboBox_1_1() {
		if (comboBox_1 == null) {
			comboBox_1 = new JComboBox();
			comboBox_1.setBounds(95, 10, 58, 21);
			for (int m = 1; m < 13; m++) {
				comboBox_1.addItem(m);
			}
		}
		return comboBox_1;
	}

	private JTextField getTextField() {
		if (textField == null) {
			textField = new JTextField();
			textField.setBounds(165, 10, 178, 21);
			textField.setColumns(10);
		}
		return textField;
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("조회");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					CalMonth();
				}
			});
			btnNewButton.setBounds(355, 10, 81, 21);
		}
		return btnNewButton;
	}

	private JButton getButton() {
		if (button == null) {
			button = new JButton("클리어");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					model.setNumRows(0);
				}
			});
			button.setBounds(550, 10, 81, 21);
		}
		return button;
	}

	private JButton getButton_1() {
		if (button_1 == null) {
			button_1 = new JButton("전체조회");
			button_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					TotalCalMonth();
				}
			});
			button_1.setBounds(448, 10, 90, 21);
		}
		return button_1;
	}

	public void TotalCalMonth() {
		
		model.setNumRows(0);
		memchul = new String[31][4];
		s = new String[32];

		int year = comboBox.getSelectedIndex() + 2010;
		int mon = comboBox_1.getSelectedIndex() + 1;
		String mont = String.valueOf(mon);
		if (mon < 10) {
			mont = "0" + mont;
		} else {};
		
		Set<String> st = mem.keySet();
		Iterator<String> it = st.iterator();
		while (it.hasNext()) {
			MemberData md = mem.get(it.hasNext());
			System.out.println("aaaaaaaaaaaaaaaaaaa");
			
			Collection<TempData> ct = chul.values();
			Iterator<TempData> it2 = ct.iterator();
			while(it.hasNext()){
				TempData td = it2.next();
				if(md.Code.equals(td.index)){
					memchul[i][0]  = td.name;
					memchul[i][1]  = td.today;
					memchul[i][2]  = String.valueOf(td.check);
					memchul[i][3]  = String.valueOf(td.leave);
					i++;
					System.out.println(memchul[i][0]);
					System.out.println(memchul[i][1]);
					System.out.println(memchul[i][2]);
					System.out.println(memchul[i][3]);
				}
			}
			s[0] = memchul[0][0];
			for(int p=0; p<31; p++){
				String[] dtday = memchul[i][1].split("-");
				rday = Integer.parseInt(dtday[2]);
				if(memchul[p][2].equals("1")){
					s[rday] = "O";
				}else { // 결근 일경우
					for (int k = 0; k < 5; k++) { //휴가 확인
						if (k == 0) {
							s[rday] = "X";
						} else {
							if (memchul[i][3].equals(k)) {
								if (k == 1) {
									s[rday] = "월차";
								} else if (k == 2) {
									s[rday] = "년차";
								} else if (k == 3) {
									s[rday] = "생리";
								} else if (k == 4) {
									s[rday] = "기타";
								}
							}
						}
					}
				}
			}
			model.addRow(s);
		}
	}
		
	
	public void CalMonth(){
		
		model.setNumRows(0);
		String[] r = new String[32];

		int year = comboBox.getSelectedIndex() + 2010;
		int mon = comboBox_1.getSelectedIndex() + 1;
		String mont = String.valueOf(mon);
		if (mon < 10) {
			mont = "0" + mont;
		} else {};
		String TYM = year + mont;
		String dexym = "[" + textField.getText() + "]" + TYM;

		Set<String> st = chul.keySet();
		Iterator<String> it = st.iterator();
		while (it.hasNext()) {
			String key = it.next();
			TempData cd = chul.get(key);

			if (key.contains(dexym)) {
				String[] dtoday = cd.today.split("-");
				Rowday = Integer.parseInt(dtoday[2]);
				if (cd.check == 1) {
					r[Rowday] = "O";
					r[0] = cd.name;
				} else {
					for (int k = 0; k < 5; k++) {
						if (k == 0) {
							r[Rowday] = "X";
						} else {
							if (cd.leave == k) {
								if (k == 1) {
									r[Rowday] = "월차";
								} else if (k == 2) {
									r[Rowday] = "년차";
								} else if (k == 3) {
									r[Rowday] = "생리";
								} else if (k == 4) {
									r[Rowday] = "기타";
								}
							}
						}
					}
				}
				System.out.println("이름 :" + r[0] + ", 날짜 :"
						+ Rowday + ", 출퇴근 휴가 유무 :" + r[Rowday]);
			}
		}
		model.addRow(r);
	}
}
